# Clear
rm(list = ls())

# Set WD
setwd("conflict_prediction_replication_pkg")

# Load Packages
source("config_code/directories.R")
source("config_code/package_requirements.R")
source("config_code/helper_functions.R")
source("config_code/model_parameters.R")

cl <- makeCluster(4)
registerDoParallel(cl)

## Run Prediction Algorithms for Table 1 and 3, and figure 1
for (country in c('indo', 'colo')) {  
  for (v in c('any', 
              'high', 
              'spike')) {
    source(paste("config_code/",country,"_data_setup.R",sep = ""))
    source(paste("config_code/",country,"_dependent_vars.R",sep = ""))
    source(paste("config_code/",country,"_predictor_vars.R",sep = ""))
    for (table in c('table1', 'table3_pt1', 'table3_pt2', 'table3_pt3',
                    'table3_pt4', 'figures_1_2_annual', 'figures_1_2_slow',
                    'figures_1_2_fixed'
                    )) {
      for (rhs.group in names(get(table))) {
        rhs <- get(table)[[rhs.group]]
        for (algo in c("lasso",
                       "gbm",
                       "rf",
                       "nn",
                       "ebma"
        )) {
          print(paste(v,rhs.group,algo,sep="---"))
          source(paste("estimation_code/main/conflict_",algo,".R",sep = ""))
        } 
      }
    }
  }
}

for (country in c('indo')) { # {indo, colo}
  for (v in c("any","high", "spike")) { # {any, high, spike}
    source(paste("config_code/",country,"_data_setup.R",sep = ""))
    source(paste("config_code/",country,"_dependent_vars.R",sep = ""))
    source(paste("config_code/",country,"_predictor_vars.R",sep = ""))
    rhs <- full_vars
    for (algo in c("lagdv",
                   "ols",
                   "ols_fe",
                   "ols_aggfe"
    )) {
      print(paste(v,algo,sep="---"))
      source(paste("estimation_code/benchmarks/conflict_",algo,".R",sep = ""))
    } 
  }
}

for (country in c('indo', 'colo')) { #'indo', 
  for (v in c('any', 'high', 'spike')) {
    source(paste("config_code/",country,"_data_setup.R",sep = ""))
    source(paste("config_code/",country,"_dependent_vars.R",sep = ""))
    source(paste("config_code/",country,"_predictor_vars.R",sep = ""))
    source("config_code/split_cross_section.R")
    for (table in c('table4', 'figure_2')) {
      for (rhs.group in names(get(table))) {
        rhs <- get(table)[[rhs.group]]
        for (algo in c("lasso",
                       "gbm",
                       "rf",
                       "nn",
                       "ebma"
        )) {
          print(paste(v,rhs.group,algo,sep="---"))
          source(paste("estimation_code/cross/conflict_",algo,"_cross.R",sep = ""))
        } 
      }
    }
  }
}

# Make all the tables and graphs
source('exhibit_code/make_all_exhibits.R')

